<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Model\ActivityMemberMod;
use Model\ActivityMemberTreasureMod;
use Model\ActivityMod;
use Model\ActivityTreasureMod;
use Model\ExchangeMod;
use Model\MemberMod;
use Model\SettingMod;
use Pub\tool\Tool;
use Pub\wx\Pay;

class Exchange extends Action
{
	private $MemberTreasure = null;
	private $ActivityTreasure = null;
	private $ActivityMember = null;
	private $Activity = null;
	private $Exchange = null;
	private $Member = null;
	private $Setting = null;

	private $_member_treasure = array();  //会员开到的宝藏
	private $_deposit = array();  //提现兑换表数据

	public function __construct()
	{
		parent::__construct();
		$this->MemberTreasure = new ActivityMemberTreasureMod();
	}

	/**
	 * 后台列表获取total
	 */
	public function total()
	{
		$this->Exchange = new ExchangeMod();
		$_where = array('uniacid'=>$this->_uniacid,'status'=>$this->_gpc['status']);
		if($this->_gpc['activity_id'] != 0) $_where['activity_id'] = $this->_gpc['activity_id'];
		$_total = $this->Exchange->total($_where);
		$this->_result = array('status'=>'success','data'=>$_total);
		$this->result($this->_result);
	}

	/**
	 * 提现列表
	 */
	public function li()
	{
		$this->Exchange = new ExchangeMod();
		$_where = array('uniacid'=>$this->_uniacid,'status'=>$this->_gpc['status']);
		if(isset($this->_gpc['uid'])) $_where['uid'] = $this->_gpc['uid'];
		$_list = $this->Exchange->findAll($_where,array('order'=>array('time','asc'),'limit'=>$this->_gpc['limit']),array('id','uid','money','status','time'));
		if($_list){
			$this->ActivityMember = new ActivityMemberMod();
			foreach($_list as $_k=>$_v){
				$_list[$_k]['time'] = date('Y-m-d H:i',$_v['time']);
				$_member = $this->uidToInfo($_v['uid']);
				$_list[$_k]['name'] = $_member['nickname'];
			}
			$this->_result = array('status'=>'success','data'=>$_list);
		}
		$this->result($this->_result);
	}

	/**
	 * 提现
	 */
	public function deposit()
	{
		$this->Exchange = new ExchangeMod();
		$this->_deposit = $this->Exchange->findOne(array('id'=>$this->_gpc['deposit_id']),array('uid','money','status'));
		if($this->_deposit['status'] != 0){
			$this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'提现错误，非法操作');
			$this->result($this->_result);
		}
		$_fans = $this->uidToFans($this->_deposit['uid']);
		$Pay = new Pay(Tool::createOrder());
		$this->Setting = new SettingMod();
		$_setting = $this->Setting->findOne(array('uniacid'=>$this->_uniacid),array('deposit_enter_detail'));
		$_result = $Pay->payer($_fans['openid'],$this->_deposit['money'],$_setting['deposit_enter_detail']);
		if($_result['status'] == 'success'){
			$this->Exchange->update(array('status'=>1),array('id'=>$this->_gpc['deposit_id']));
		}
		$this->result($_result);
	}

	/**
	 * 拒绝提现
	 */
	public function refuse()
	{
		$this->Exchange = new ExchangeMod();
		$this->_deposit = $this->Exchange->findOne(array('id'=>$this->_gpc['deposit_id']),array('uid','money','status'));
		if($this->_deposit['status'] != 0){
			$this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'非法操作');
			$this->result($this->_result);
		}
		$this->Member = new MemberMod();
		$this->Member->math(array('money +'=>$this->_deposit['money']),array('uid'=>$this->_deposit['uid'],'uniacid'=>$this->_uniacid));
		$this->Exchange->update(array('status'=>2),array('id'=>$this->_gpc['deposit_id']));
		$this->_result = array('status'=>'success');
		$this->result($this->_result);
	}

	/**
	 * 删除提现
	 */
	public function delete()
	{
		$this->Exchange = new ExchangeMod();
		$this->_deposit = $this->Exchange->findOne(array('id'=>$this->_gpc['deposit_id']),array('uid','money','status'));
		if($this->_deposit['status'] == 0 || $this->_deposit['status']==9){
			$this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'非法操作');
			$this->result($this->_result);
		}
		$_result = $this->Exchange->update(array('status'=>9),array('id'=>$this->_gpc['deposit_id']));
		if(!$_result){
			$this->_result = array('status'=>'error','err_status'=>8,'err_msg'=>'删除失败');
			$this->result($this->_result);
		}
		$this->_result = array('status'=>'success');
		$this->result($this->_result);
	}

	/**
	 * 宝藏列表
	 */
	public function treasure()
	{
		$_treasure = $this->MemberTreasure->findAll(array('uid'=>$this->_gpc['uid'],'activity_id'=>$this->_gpc['activity_id'],'number >'=>0,'type'=>1),array(),array('treasure_id','number','exchange'));
		if($_treasure){
			$this->ActivityTreasure = new ActivityTreasureMod();
			foreach($_treasure as $_k=>$_v){
				$_bz = $this->ActivityTreasure->findOne(array('id'=>$_v['treasure_id']),array('name','icon','money'));
				$_treasure[$_k]['name'] = $_bz['name'];
				$_treasure[$_k]['icon'] = tomedia($_bz['icon']);
				$_treasure[$_k]['money'] = $_bz['money'];
			}
			$this->_result = array('status'=>'success','data'=>$_treasure);
		}
		$this->result($this->_result);
	}

	/**
	 * 用户余额
	 */
	public function balance()
	{
		$_member_treasure = $this->MemberTreasure->findAll(array('uid'=>$this->_gpc['uid'],'activity_id'=>$this->_gpc['activity_id'],'exchange'=>0,'type'=>1),array('order'=>array('id','desc')),array('number','treasure_id','id'));
		if($_member_treasure){
			$_balance = 0;
			$this->ActivityTreasure = new ActivityTreasureMod();
			foreach($_member_treasure as $_k=>$_v){
				$_treasure = $this->ActivityTreasure->findOne(array('id'=>$_v['treasure_id']),array('money'));
				$_balance += $_treasure['money'] * $_v['number'];
			}
			$this->_result = array('status'=>'success','data'=>$this->makeMoney($_balance));
		}else{
			$this->_result = array('status'=>'success','data'=>'0.00');
		}
		$this->result($this->_result);
	}

	/**
	 * 将一个数字转为金钱数字
	 * @param $_number
	 * @return float|string
	 */
	private function makeMoney($_number)
	{
		if(ceil($_number) == $_number){
			return $_number.'.00';
		}else{
			$_numArr = explode('.',$_number);
			$_decimals = end($_numArr);
			if(strlen($_decimals) == 1){
				return $_numArr[0].'.'.$_decimals.'0';
			}else if(strlen($_decimals) == 2){
				return $_number;
			}else{
				return round($_number,2);
			}
		}
	}

	/**
	 * 给用户兑换余额
	 */
	public function exchange()
	{
		$this->Activity = new ActivityMod();
		$_balance = $this->getBalance();
		if($_balance<=0){
			$this->_result = array('status'=>'error','err_status'=>8,'err_msg'=>'可兑换余额不足');
			$this->result($this->_result);
		}
		foreach($this->_member_treasure as $_v){
			$this->MemberTreasure->update(array('exchange'=>$_v['number']),array('id'=>$_v['id']));
		}
		$this->Member = new MemberMod();
		$_result = $this->Member->math(array('money +'=>$_balance),array('uid'=>$this->_gpc['uid']));
		if($_result){
			$this->_result = array('status'=>'success');
		}else{
			foreach($this->_member_treasure as $_v){
				$this->MemberTreasure->update(array('exchange'=>$_v['exchange']),array('id'=>$_v['id']));
			}
			$this->_result = array('status'=>'error','err_status'=>7,'err_msg'=>'兑换余额失败');
		}
		$this->result($this->_result);
	}

	/**
	 * 用户可兑换余额
	 */
	private function getBalance()
	{
		$_member_treasure = $this->MemberTreasure->findAll(array('uid'=>$this->_gpc['uid'],'activity_id'=>$this->_gpc['activity_id'],'type'=>1),array('order'=>array('id','desc')),array('exchange','number','treasure_id','id'));
		if($_member_treasure){
			$_balance = 0;
			$this->ActivityTreasure = new ActivityTreasureMod();
			foreach($_member_treasure as $_k=>$_v){
			    $_number = $_v['number'] - $_v['exchange'];
			    if($_number > 0){
			        $this->_member_treasure[] = $_v;
                    $_treasure = $this->ActivityTreasure->findOne(array('id'=>$_v['treasure_id']),array('money'));
                    $_balance += $_treasure['money'] * $_number;
                }
			}
			return $_balance;
		}else{
			return 0;
		}
	}

	/**
	 * 批量提现
	 */
	public function allDeposit()
	{
		$this->Exchange = new ExchangeMod();
		$_success = 0;
		$_error = 0;
		foreach($this->_gpc['data'] as $_v){
			if($this->oneDeposit($_v)){
				$_success ++;
			}else{
				$_error ++;
			}
		}
		$this->_result = array('status'=>'success','data'=>'成功提现'.$_success.'条，失败提现'.$_error.'条。');
		$this->result($this->_result);
	}

	/**
	 * 提现
	 * @param $_deposit_id  提现id
	 */
	private function oneDeposit($_deposit_id)
	{
		$this->_deposit = $this->Exchange->findOne(array('id'=>$_deposit_id),array('uid','money','status'));
		if($this->_deposit['status'] != 0){
			$this->_result = array('status'=>'error','err_status'=>9,'err_msg'=>'提现错误，非法操作');
			return false;
		}
		$_fans = $this->uidToFans($this->_deposit['uid']);
		$Pay = new Pay(Tool::createOrder());
        $this->Setting = new SettingMod();
        $_setting = $this->Setting->findOne(array('uniacid'=>$this->_uniacid),array('deposit_enter_detail'));
		$_result = $Pay->payer($_fans['openid'],$this->_deposit['money'],$_setting['deposit_enter_detail']);
		if($_result['status'] == 'success'){
			return $this->Exchange->update(array('status'=>1),array('id'=>$_deposit_id));
		}else{
			return false;
		}
	}
}